package string;

import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author: ycy
 * @CreateDate: 2019/4/15 12:15
 */
public class regex {

  public static void main(String[] args) {

    String[] addressArr = {"施工单位视频河北省石家庄市新华区合作路街道电大街66-68",
            "石家庄市新华区合作路街道电大街66-68",
            "新华区合作路街道电大街66-68",
            "新华合作路街道电大街66-68"

    };

    // 正则表达式：[\u4e00-\u9fa5]用于匹配中文字符
    String regex = "(?:.*?(省|自治区|市))?(?:.*?(市))?.*?([\\u4e00-\\u9fa5]+区|县)";
    Pattern pattern = Pattern.compile(regex);

    for (String address : addressArr) {
      Matcher matcher = pattern.matcher(address);
      if (matcher.find()) {
        String district = matcher.group(3); // 第三个分组为区县
        System.out.println("地址：" + address + " → 区县：" + district);
      }
    }

//    for (String address : addressArr) {
//      String regex = ".*?(省|自治区|直辖市)(.*?市)(.*?[区县]).*"; // 匹配省市县区层级
//      Pattern pattern = Pattern.compile(regex);
//      Matcher matcher = pattern.matcher(address);
//
//      if (matcher.find()) {
//        String city = matcher.group(2).trim();   // 提取市，如“石家庄市”
//        String district = matcher.group(3).trim(); // 提取区，如“新华区”
//        System.out.println("市：" + city);
//        System.out.println("区：" + district);
//      }
//    }


  }


  @Test
  public void test() {
    String regex = "[-+]?([0-9]*.)?[0-9]*";
    boolean matches = Pattern.matches(regex,"2.1");
    System.out.println(matches);
  }

  @Test
  public void test1() {

    Pattern p = Pattern.compile("cat"); //keyword
    Matcher m = p.matcher("one cat two cats in the yard");
    StringBuffer sb = new StringBuffer();
    while (m.find()) {
      m.appendReplacement(sb, "dog");
    }
    m.appendTail(sb);
    System.out.println(sb.toString());
    System.out.println(new regex().sepatorTagNames("性价比高|900航延|30万意外{SPS}{SPS}")
    );


    String queryStr = null;

    queryStr = "(Type:district) AND keyword:\" \"".replaceAll("keyword:\".+\"",
            "((   keyword:\"^#上海\"^100   OR   multikeyword:\"上海\"^1   ) AND rankscore>1 AND districtpath:\"2\" )");
    System.out.println(queryStr);

  }

  private String sepatorTagNames(String tagNames) {
    Set<String> set = Sets.newTreeSet();
    if (StringUtils.isNotBlank(tagNames)) {
      String[] split = tagNames.split("\\{SPS}|\\|");
      for (String s : split) {
        if (StringUtils.isNotBlank(s)) {
          set.add(s.trim());
        }
      }
      if (set.isEmpty()) {
        return null;
      }
      return StringUtils.join(set.iterator(), ",");
    }
    return tagNames;
  }


  private static final String SPECIAL_CHAR_REG = "[\\\\`'｀丶~!@#$%^&�*()=•|、丨{}:;\\[\\].<>?！￥…（）—【】‘；：”“’。，？￼\n\r\t]";

  private static final Pattern SPECIAL_CHAR_PATTERN = Pattern.compile(SPECIAL_CHAR_REG);

  @Test
  public void test2() {

//    System.out.println(filterKeyword("丨"));
    System.out.println(filterKeyword("haha丨 麻煩"));

  }


  /**
   * 搜索词过滤
   */
  public static String filterKeyword(String keyword) {
    keyword = keyword.trim().replaceAll("\"", " ");
    keyword = keyword.replaceAll(" +", " ");
    if (!hasEnglish(keyword)) {
      keyword = keyword.replaceAll(" ", "");
    }
    Matcher m = SPECIAL_CHAR_PATTERN.matcher(keyword);
    return m.replaceAll("").trim();
  }


  /**
   * 有英文关键词
   */
  public static boolean hasEnglish(String keyword) {
    if (StringUtils.isEmpty(keyword)) {
      return false;
    }

    keyword = keyword.replaceAll("\\s", "").toLowerCase();
    for (char c : keyword.toCharArray()) {
      if (c >= 'a' && c <= 'z') {
        return true;
      }
    }
    return false;
  }

  @Test
  public void test3() {

//    System.out.println(filterKeyword("丨"));
    System.out.println("{ }".replaceFirst("}",">0}"));
    Integer bl = null;
    Boolean b = bl == 0;
  }

}



